package leetcode.offer_two_special.queue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author mazouri
 * @create 2021-09-11 16:22
 */
public class MovingAverage {
    Queue<Integer> queue;
    int capacity;
    int sum;

    /**
     * Initialize your data structure here.
     */
    public MovingAverage(int size) {
        queue = new LinkedList<>();
        this.capacity = size;
        this.sum = 0;
    }

    public double next(int val) {
        queue.offer(val);
        sum += val;
        if (queue.size() > capacity) sum -= queue.poll();

        return sum * 1.0 / queue.size();
    }
}
